package com.niiwoo.civet.account.service.local.withdraw;/**
 * Created by DELL on 2017/12/5.
 */

import com.niiwoo.civet.account.dao.entity.WithdrawConfirmOrder;
import com.niiwoo.civet.account.dao.entity.WithdrawConfirmOrderExample;
import com.niiwoo.civet.account.dao.mapper.WithdrawConfirmOrderMapper;
import com.niiwoo.civet.account.enums.WithdrawConfirmOrderStatusEnum;
import com.niiwoo.tripod.lanmao.component.LanMaoSequence;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 提现确认订单表Service
 *
 * @author xiaoyingping
 * @create 2017-12-05 17:31
 **/
@Service
public class WithdrawConfirmOrderService {

    @Autowired
    private WithdrawConfirmOrderMapper withdrawConfirmOrderMapper;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private LanMaoSequence lanMaoSequence;

    /**
     * 查询提现确认订单
     * @param requestNo
     * @return
     */
    public WithdrawConfirmOrder getByRequestNo(String requestNo) {
        WithdrawConfirmOrderExample example = new WithdrawConfirmOrderExample();
        WithdrawConfirmOrderExample.Criteria criteria = example.createCriteria();
        criteria.andRequestNoEqualTo(requestNo);

        List<WithdrawConfirmOrder> withdrawConfirmOrderList = withdrawConfirmOrderMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(withdrawConfirmOrderList)) {
            return null;
        }
        
        return withdrawConfirmOrderList.get(0);
    }


    /**
     * 新增提现确认订单记录
     *
     * @param withdrawConfirmOrder
     */
    public void addWithdrawConfirmOrder(WithdrawConfirmOrder withdrawConfirmOrder) {
        withdrawConfirmOrder.setId(snowflakeIdWorker.nextId());
        withdrawConfirmOrder.setRequestNo(lanMaoSequence.getRequestNo());
        Date date = new Date();
        withdrawConfirmOrder.setCreateTime(date);
        withdrawConfirmOrder.setUpdateTime(date);
        withdrawConfirmOrderMapper.insertSelective(withdrawConfirmOrder);
    }

    /**
     * 更新提现确认订单的状态
     *
     * @param withdrawConfirmOrderId
     * @param statusEnum
     * @param updateUserId
     * @return
     */
    public int updateStatus(Long withdrawConfirmOrderId, WithdrawConfirmOrderStatusEnum statusEnum, Long updateUserId) {
        Assert.notNull(withdrawConfirmOrderId, "withdrawConfirmOrderId不能为空");
        Assert.notNull(statusEnum, "statusEnum不能为空");
        Assert.notNull(updateUserId, "updateUserId不能为空");

        //修改状态
        WithdrawConfirmOrder record = new WithdrawConfirmOrder();
        record.setId(withdrawConfirmOrderId);
        record.setStatus(statusEnum.getCode());
        record.setUpdateManager(updateUserId);
        record.setUpdateTime(new Date());
        return withdrawConfirmOrderMapper.updateByPrimaryKeySelective(record);
    }


}
